package com.gdit.zax.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.dao.CommentDao;
import com.gdit.zax.entitys.Comment;
import com.gdit.zax.entitys.CommentAll;
import com.gdit.zax.entitys.UserInfo;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.LogUtil;
import com.gdit.zax.utils.SqlUtil;

public class CommentDaoImpl implements CommentDao {
	private PreparedStatement ps;
	private ResultSet rs;

	private Connection conn;
	private HttpServletRequest request;
	private HttpServletResponse response;

	public CommentDaoImpl(Connection conn, HttpServletRequest request,
			HttpServletResponse response) {
		this.conn = conn;
		this.request = request;
		this.response = response;
	}

	public int getUseSendCommentSum(int uid) {
		String sql = "select count(Id) from t_comment where UserId=? and ReCommentId=-1";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);

			rs = ps.executeQuery();
			if (rs.next()) {
				return rs.getInt(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	public boolean sendComment(int uid, int vid, String connent) {
		String sql = "insert t_comment set ReCommentId=?,UserId=?,ToUserId=?,Content=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, -1);
			ps.setInt(2, uid);
			ps.setInt(3, -1);
			ps.setString(4, connent);

			int res = ps.executeUpdate();
			if (res > 0) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	

	public boolean sendReComment(int cid,int uid, int touid, String connent) {
		String sql = "insert t_comment set ReCommentId=?,UserId=?,ToUserId=?,Content=?,ToUserName=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, cid);
			ps.setInt(2, uid);
			ps.setInt(3, touid);
			ps.setString(4, connent);
			UserInfo uinfo = DAOFactory.init(conn, request, response).getUserDao().getInfo(touid);
			ps.setString(5, uinfo==null?"noone":uinfo.getNick());
			
			int res = ps.executeUpdate();
			if (res > 0) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public int getNotReadCommentSum(int uid) {
		// TODO Auto-generated method stub
		return 0;
	}

	public int getUseCommentSum(int uid) {
		String sql = "select count(Id) from t_comment where UserId=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);

			rs = ps.executeQuery();
			if (rs.next()) {
				return rs.getInt(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	public ArrayList<CommentAll> listMySendComment(int uid, int page,
			int onepagecount) {
		String sql = SqlUtil.getSql("listCommemtAll.sql");
		ArrayList<CommentAll> list = new ArrayList<CommentAll>();
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			ps.setInt(2, (page - 1) * onepagecount);
			ps.setInt(3, onepagecount);

			rs = ps.executeQuery();
			while (rs.next()) {
				list.add(DBUtil.getJoinObject(rs, CommentAll.class));
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return list;
	}

	public Comment getCommentInfo(int cid) {
		String sql = "select * from t_comment where id=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, cid);

			rs = ps.executeQuery();
			while (rs.next()) {
				return DBUtil.getobject(rs, Comment.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public boolean delComment(int cid) {
		String sql = "delete from t_comment where id=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, cid);

			int res = ps.executeUpdate();
			if (res != 0) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public ArrayList<CommentAll> listVideoComment(int cid, int page,
			int onepagecount) {
		String sql = SqlUtil.getSql("listVideoCommemtAll.sql");
		ArrayList<CommentAll> list = new ArrayList<CommentAll>();
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, cid);
			ps.setInt(2, (page - 1) * onepagecount);
			ps.setInt(3, onepagecount);

			rs = ps.executeQuery();
			while (rs.next()) {
				list.add(DBUtil.getJoinObject(rs, CommentAll.class));
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return list;
	}

	public ArrayList<CommentAll> listCommentSub(int cid, int page,int onepagecount) {
		String sql = SqlUtil.getSql("listCommemtSubAll.sql");
		ArrayList<CommentAll> list = new ArrayList<CommentAll>();
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, cid);
			ps.setInt(2, (page - 1) * onepagecount);
			ps.setInt(3, onepagecount);

			rs = ps.executeQuery();
			while (rs.next()) {
				list.add(DBUtil.getJoinObject(rs, CommentAll.class));
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return list;
	}

	public boolean sendDiscus(int uid, int cid, String content) {
		String sql = "insert t_comment set ReCommentId=?,UserId=?,ToUserId=?,Content=?,CourseId=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, -1);
			ps.setInt(2, uid);
			ps.setInt(3, -1);
			ps.setString(4, content);
			ps.setInt(5, cid);

			int res = ps.executeUpdate();
			if (res > 0) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

}
